<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生学号" prop="studentId">
        <el-input
          v-model="queryParams.studentId"
          placeholder="请输入学生学号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="studentPhone">
        <el-input
          v-model="queryParams.studentPhone"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="离校时间" prop="studentLeavingTime">
        <el-date-picker clearable
                        v-model="queryParams.studentLeavingTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择离校时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeProveCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="proveStatus">
        <el-select v-model="queryParams.proveStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.glsins_prove_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gradu_info:leave_sup_manager:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gradu_info:leave_sup_manager:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gradu_info:leave_sup_manager:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gradu_info:leave_sup_manager:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="leave_sup_managerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="证明ID" align="center" prop="proveId"/>
      <el-table-column label="学生学号" align="center" prop="studentId"/>
      <el-table-column label="学生姓名" align="center" prop="studentName"/>
      <el-table-column label="联系方式" align="center" prop="studentPhone"/>
      <el-table-column label="离校时间" align="center" prop="studentLeavingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.studentLeavingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="证明内容" align="center" width="150">
        <template slot-scope="scope">
          <el-button type="primary" round size="mini" @click="openTan(scope.row.content)" >详情</el-button>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="proveCreateTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.proveCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核员ID" align="center" prop="auditorId" v-if="false"/>
      <el-table-column label="审核员姓名" align="center" prop="auditorName" v-if="false"/>
      <el-table-column label="审核状态" align="center" prop="proveStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.glsins_prove_status" :value="scope.row.proveStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gradu_info:leave_sup_manager:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gradu_info:leave_sup_manager:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改离校申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId" v-show="false">
          <el-input v-model="form.userId=this.$store.state.user.userid" placeholder="请输入用户ID"></el-input>
        </el-form-item>
        <el-form-item label="学生学号" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学生学号" :minlength="12" :maxlength="12" show-word-limit clearable
                    prefix-icon='el-icon-warning-outline' :disabled="isDisabledFlag" />
        </el-form-item>
        <el-form-item label="学生姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入学生姓名" prefix-icon="el-icon-user" clearable :disabled="isDisabledFlag" />
        </el-form-item>
        <el-form-item label="联系方式" prop="studentPhone">
          <el-input v-model="form.studentPhone" placeholder="请输入联系方式" :maxlength="11"
                    show-word-limit clearable prefix-icon='el-icon-mobile' :disabled="isDisabledFlag" />
        </el-form-item>
        <el-form-item label="离校时间" prop="studentLeavingTime">
          <el-date-picker clearable
                          v-model="form.studentLeavingTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择离校时间" :disabled="isDisabledFlag" >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="证明内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" :rows="2" :disabled="isDisabledFlag"/>
        </el-form-item>
        <el-form-item label="审核员ID" prop="auditorId" v-if="false">
          <el-input v-model="form.auditorId" placeholder="请输入审核员ID"/>
        </el-form-item>
        <el-form-item label="审核员姓名" prop="auditorName" v-if="false">
          <el-input v-model="form.auditorName" placeholder="请输入审核员姓名"/>
        </el-form-item>
        <el-form-item label="审核状态" prop="proveStatus" v-if="checkRole(['admin','teacher'])" >
          <el-radio-group v-model="form.proveStatus">
            <el-radio
              v-for="dict in dict.type.glsins_prove_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listLeave_sup_manager,
  getLeave_sup_manager,
  delLeave_sup_manager,
  addLeave_sup_manager,
  updateLeave_sup_manager
} from "@/api/gradu_info/leave_sup_manager";

import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数

export default {
  name: "Leave_sup_manager",
  dicts: ['glsins_prove_status'],
  data() {
    return {
      // 证明内容弹窗
      contentDialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 离校申请表格数据
      leave_sup_managerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核状态时间范围
      daterangeProveCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        studentId: null,
        studentName: null,
        studentPhone: null,
        studentLeavingTime: null,
        proveCreateTime: null,
        proveStatus: null
      },
      // 判断是否被禁止
      isDisabledFlag:checkRole(['admin','teacher']),
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        studentId: [
          {required: true, message: "学生学号不能为空", trigger: "blur"}
        ],
        studentName: [
          {required: true, message: "学生姓名不能为空", trigger: "blur"}
        ],
        studentPhone: [
          {required: true, message: "联系方式不能为空", trigger: "blur"}
        ],
        studentLeavingTime: [
          {required: true, message: "离校时间不能为空", trigger: "blur"}
        ],
        proveCreateTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        proveStatus: [
          {required: true, message: "审核状态不能为空", trigger: "change"}
        ]
      }
    };
  },
  created() {
    this.getList();
    console.log("当前用户ID：" + this.$store.state.user.userid);
    console.log("当前是否为admin/teacher组："+checkRole(['admin','teacher']));
    console.log("当前是否为admin/teacher组："+this.$auth.hasRole("student"));
  },
  methods: {
    openTan(val) {
      this.$alert(val, '详情内容', {
        confirmButtonText: '确定',
        // callback: action => {
        //   this.$message({
        //     type: 'success',
        //     message: `关闭详情`
        //   });
        // }
      });
    },
    /*权限鉴权*/
    checkPermi,
    checkRole,

    /** 查询离校申请列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};

      if (null != this.daterangeProveCreateTime && '' != this.daterangeProveCreateTime) {
        this.queryParams.params["beginProveCreateTime"] = this.daterangeProveCreateTime[0];
        this.queryParams.params["endProveCreateTime"] = this.daterangeProveCreateTime[1];
      }
      // if (!checkRole(['admin','teacher'])){
      //   this.queryParams.userId = this.$store.state.user.userid;
      // }

      listLeave_sup_manager(this.queryParams).then(response => {
        this.leave_sup_managerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        proveId: null,
        userId: null,
        studentId: null,
        studentName: null,
        studentPhone: null,
        studentLeavingTime: null,
        content: null,
        proveCreateTime: null,
        auditorId: null,
        auditorName: null,
        proveStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeProveCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.proveId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加离校申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const proveId = row.proveId || this.ids
      getLeave_sup_manager(proveId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改离校申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.proveId != null) {
            // if (checkRole(['admin','teacher'])){
            //   this.form.auditorId = this.$store.state.user.userId;
            //   this.form.auditorName = this.$store.state.user.userName;
            // }
            updateLeave_sup_manager(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLeave_sup_manager(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const proveIds = row.proveId || this.ids;
      this.$modal.confirm('是否确认删除离校申请编号为"' + proveIds + '"的数据项？').then(function () {
        return delLeave_sup_manager(proveIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gradu_info/leave_sup_manager/export', {
        ...this.queryParams
      }, `leave_sup_manager_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
